package com.example.chenfenweb.controller;

import java.util.List;

import com.example.chenfenweb.config.WebConfig;
import com.example.chenfenweb.entity.SysTemplateManager;
import com.example.chenfenweb.service.ISysTemplateManagerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Description: 模板管理
 * @Author: jeecg-boot
 * @Date:   2024-06-22
 * @Version: V1.0
 */
@RestController
@RequestMapping("/sys")
@Slf4j
public class SysTemplateManagerController   {

	@Autowired
	private ISysTemplateManagerService sysTemplateManagerService;


	@GetMapping(value = "/list")
	public  String list() {
		System.out.println("============");
		List<SysTemplateManager> list = sysTemplateManagerService.list();
		System.out.println(list);
		return "123456";
	}

	 private final ApplicationContext applicationContext;

	 public SysTemplateManagerController(ApplicationContext applicationContext) {
		 this.applicationContext = applicationContext;
	 }


	 @GetMapping("/reload")
	 public String reloadWebMvcConfigurer() {
		 ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) applicationContext;
		 // 动态注册一个新的WebMvcConfigurer
//		 configurableApplicationContext.getBeanFactory().registerSingleton("webConfig", new WebConfig());
		 // 触发重新配置
//		 configurableApplicationContext.getBean(WebMvcConfigurerComposite.class)
//				 .setConfigurers(configurableApplicationContext.getBeansOfType(WebConfig.class).values());

		 // 获取bean工厂并转换为DefaultListableBeanFactory
		 DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) configurableApplicationContext.getBeanFactory();
		 defaultListableBeanFactory.removeBeanDefinition("webConfig");
		 // 通过BeanDefinitionBuilder创建bean定义
		 BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(WebConfig.class);
		 // 注册bean
		 defaultListableBeanFactory.registerBeanDefinition("webConfig", beanDefinitionBuilder.getRawBeanDefinition());
		 return "webConfig reloaded";
	 }


	 @GetMapping(value = "/refash")
	 public  String refash() {
		 System.out.println("============");
		 List<SysTemplateManager> list = sysTemplateManagerService.list();
		 System.out.println(list);
		 return "123456";
	 }








 }
